﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.ResourceType
{
    public partial class ResourceType_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_RESTYPE bllResourceType = new BLL_T_SYS_RESTYPE();
        private Model_T_SYS_RESTYPE modelResourceType = new Model_T_SYS_RESTYPE();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                ControlTextBox(true, true, true);
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定树性菜单
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            DataSet ds = bllResourceType.GetAllList();
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView1, "NAME", "restypeid", false);
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            string resourceId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            txtName.Value = TreeView1.SelectedNode.Text;
            modelResourceType = bllResourceType.GetModel(resourceId);
            if (modelResourceType != null)
            {
                txtName.Value = modelResourceType.NAME;
                txtTableName.Value = modelResourceType.TABLE_NAME;
            }

            ControlTextBox(true, true,false);
            HiddenResourceId.Value = resourceId;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false,true);
            HiddenResourceId.Value = "";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string sourceId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            bool result = bllResourceType.Delete(sourceId);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                ControlTextBox(false, false,true);
                BindTreeView();
                HiddenResourceId.Value = "";
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false, false);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (HiddenResourceId.Value == "")
            {
                #region 添加
                modelResourceType.CREATE_TIME = DateTime.Now;
                modelResourceType.CREATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelResourceType.CREATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelResourceType.NAME = txtName.Value.Trim();
                modelResourceType.UPDATE_TIME = DateTime.Now;
                modelResourceType.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelResourceType.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelResourceType.TABLE_NAME = txtTableName.Value.Trim();
                bllResourceType.Add(modelResourceType);
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "添加成功！");
                BindTreeView();
                ControlTextBox(true, true, true);
            }
            else
            {
                modelResourceType = bllResourceType.GetModel(HiddenResourceId.Value);
                #region 更新
                if (string.IsNullOrEmpty(modelResourceType.CREATOR_ID))
                    modelResourceType.CREATOR_ID = "";
                if (string.IsNullOrEmpty(modelResourceType.CREATOR_NAME))
                    modelResourceType.CREATOR_NAME = "";
                modelResourceType.NAME = txtName.Value.Trim();
                modelResourceType.UPDATE_TIME = DateTime.Now;
                modelResourceType.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelResourceType.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelResourceType.TABLE_NAME = txtTableName.Value.Trim();
                bllResourceType.Update(modelResourceType);

                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "更新成功！");
                BindTreeView();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ControlTextBox(true, true, true);
        }

        /// <summary>
        /// 控制文本框
        /// </summary>
        /// <param name="txtNameDisabled"></param>
        /// <param name="txtCodeDisabled"></param>
        /// <param name="boolClear"></param>
        private void ControlTextBox(bool txtNameDisabled, bool txtTableNameDisabled,bool boolClear)
        {
            if (boolClear)
            {
                txtName.Value = "";
                txtTableName.Value = "";
            }
            txtName.Disabled = txtNameDisabled;
            txtTableName.Disabled = txtNameDisabled;
        }
    }
}